package com.geekaca.jsp;

import com.geekaca.jsp.pojo.Brand;
import com.geekaca.jsp.pojo.BrandType;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/edit")
public class EditServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(" edit service...");

//        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        String brandName = req.getParameter("brandName");
        System.out.println("brand_name :" + brandName);

        String companyName = req.getParameter("companyName");
        String description = req.getParameter("description");

        // 获得type
        String brandTypeName = req.getParameter("brandType");

        Integer type_id;
        switch (brandTypeName) {
            case "食品":
                type_id = 1;
                break;
            case "数码":
                type_id = 2;
                break;
            case "文化":
                type_id = 3;
                break;

            default:
                type_id = 1;
        }

        String ordered = req.getParameter("ordered");
        System.out.println("ordered :" + ordered);
        Integer orderInt = Integer.parseInt(ordered);

        String status = req.getParameter("status");
        System.out.println("status: " + status);
        Integer statusInt = Integer.parseInt(status);

        BrandType brandType;

        switch (type_id) {
            case 1:
                brandType = new BrandType(1, 1, "食品");
                break;
            case 2:
                brandType = new BrandType(2, 2, "数码");
                break;
            case 3:
                brandType = new BrandType(3, 3, "文化");
                break;
            default:
                brandType = new BrandType();
        }

        Brand brand = new Brand(i, brandName, companyName, orderInt, description, statusInt, brandType);
        req.setAttribute("brand", brand);
        req.getRequestDispatcher("/updateBrand.jsp").forward(req, resp);
    }
}
